var location = require("../../../mybehaviors/location/location");
var app = getApp()
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    Height: "",
    bannerHeight: "",
    type_val: 1,
    category_id: "",
    address: {},
    banner: [],
    activity: [],
    button: [],
    goods_category: [],
    goodsList: [],
    agent: null,
    page: 1,
    total: 0,
    finished: false,
    isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getLocation((res) => { 
      this.setData({
        address: res  
      })
      this.getData(); 
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoading) return
    this.getGoodsList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  imgBannerHeight(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = (winWid * imgh / imgw - 24 )+ "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      bannerHeight: swiperH //设置高度
    });
  },
  imgHeight(e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = (winWid * imgh / imgw - 24 )+ "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      Height: swiperH //设置高度
    });
  },
  splitArr (arr,num) {
    var res=[];
    for (var i = 0,len = arr.length; i < len; i+=num) {
      res.push(arr.slice(i, i+num));
    }
    return res
  },
  clickType(ev) {
    if(this.data.isLoading) return
    this.setData({
      [ev.currentTarget.dataset.fields]: ev.currentTarget.dataset.value
    })
    this.initData();
    this.getGoodsList();
  },
  goDw(e) {
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=signHome',
    });
  },
  goSearchPage() {
    wx.navigateTo({
      url: '/mircoApp/expressSignature/search/search',
    });
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.supplier-local-life.frontend.index.index'),
      data: { adcode: this.data.address.ad_info.adcode },
      success: ({data}) => {
        if(data.result) {
          let goods_category = data.data.goods_category;
          this.setData({
            banner: data.data.banner,
            activity: data.data.activity,
            button: this.splitArr(data.data.button, 10),
            goods_category,
            agent: data.data.agent,
            category_id: data.data.goods_category[0].id,
            config: data.data.config
          });
          this.getGoodsList();
          wx.setNavigationBarTitle({
            title: data.data.shop_name,
          })
        }else {
          wx.showToast({
            icon: 'none',
            title: data.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  getGoodsList() {
    if (this.data.finished) return;
    this.setData({
      isLoading: true
    })
    let url = this.data.type_val == 1 ? "plugin.supplier-local-life.frontend.index.get-goods-list" : "plugin.supplier-local-life.frontend.index.get-supplier-store-list";
    let form = this.data.type_val == 1 ? {
      adcode: this.data.address.ad_info.adcode,
      category_id: this.data.category_id,
      page: this.data.page
    } : {
      adcode: this.data.address.ad_info.adcode,
      page: this.data.page
    };
    app._postNetWork({
      url: app.getNetAddresss(url),
      data: form,
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let goodsList = [];
        let total = 1;
        goodsList = this.data.goodsList.concat(res.data);
        if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
          finished = true;
        }
        total = res.total;
        this.setData({
          finished,
          goodsList,
          page: ++this.data.page,
          total,
          isLoading: false
        });
      }
    });
  },
  initData() {
    this.setData({
      goodsList: [],
      page: 1,
      total: 0,
      finished: false
    })
  },
  goPage(ev) {
    if(ev.currentTarget.dataset.mini_url) {
      wx.navigateTo({
        url: ev.currentTarget.dataset.mini_url
      });
    }
  },
  goGoodsDetail(ev) {
    wx.navigateTo({
      url: `/packageA/detail_v2/detail_v2?id=${ev.currentTarget.dataset.id}`,
    });
  },
  goEnterShop(ev) {
    wx.navigateTo({
      url:  `/packageA/member/supplier/SupplierShop/SupplierShop?id=${ev.currentTarget.dataset.id}`
    });
  }
})